<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use app\models\ClassItem;
use app\models\User;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;

class SiteController extends Controller
{
    public $layout = 'mainindex';
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    public function actionIndex()
    {
        $sql = "select t2.job_name,t1.* from t_data_job_list t1 left JOIN dw_data_jobs t2 on t1.job_id = t2.id  where date_line = (select max(date_line) from  t_data_job_list) order by total_num desc limit 30 ";

        $jobsData = Yii::$app->db->createCommand($sql)->queryAll();
        return $this->render('index', [
            'jobsData'=>$jobsData,
        ]);
    }

    public function actionLogin()
    {
        if (!\Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        }
        return $this->render('login', [
            'model' => $model,
        ]);
    }

    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }

    public function actionContact()
    {
        $model = new ContactForm();
        if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
            Yii::$app->session->setFlash('contactFormSubmitted');

            return $this->refresh();
        }
        return $this->render('contact', [
            'model' => $model,
        ]);
    }

    public function actionAbout()
    {
        return $this->render('about');
    }
    public function actionClass()
    {
        if(isset(Yii::$app->request->get()['id'])){
            $class = Yii::$app->request->get()['id'];
        }else{
            $class = Yii::$app->user->identity->class;
        }
        $uids = Yii::$app->db->createCommand("select id,truename,student_id from user where class = '{$class}'")->queryAll();
        $temp = array();
        foreach($uids as $uid){
            $rate = User::getRatesForUserByUid($uid['id'])['avg'];
            $uid['rate'] = $rate;
            $temp[$rate.$uid['student_id']]=$uid;
        }
        krsort($temp);

        return $this->render('class', [
            'data' => $temp
        ]);
    }
}
